package LC;

public class LC1004 {
    public static void main(String[] args) {
        System.out.println(longestOnes(new int[]{0, 0, 0, 1}, 4));
    }
    public static int longestOnes(int[] nums, int k) {
        int n0=0;
        int ans =0,temp =0,left =0 ;
        for (int num : nums) {
            temp++;
            if (num == 0) {
                n0++;
            }
            if (n0 <= k) {
                ans = Math.max(ans, temp);
            }
            if (n0 > k) {
                if (nums[left] == 0) {
                    n0--;
                }
                temp--;
                left++;
            }
        }
        return ans;
    }
}
